package _220325;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author ShadowLim
 * @create 2022-03-25-11:33
 */
public class 大胖子走迷宫 {
    static int n, k;
    static char[][] map;
    static int[][] dirs = {{1,0}, {-1, 0}, {0, 1}, {0, -1}};
    static boolean[][] vis;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        k = scanner.nextInt();
        map = new char[n][n];
        vis = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            map[i] = scanner.next().toCharArray();
        }
        System.out.println(bfs());
        scanner.close();
    }

    private static int bfs() {
        Queue<Node> q = new LinkedList<>();
//        Queue<Node> q = new ArrayDeque<>();
        q.offer(new Node(2, 2, 0));
        vis[2][2] = true;
        while (!q.isEmpty()) {
            Node node = q.poll();
            int time = node.time;
            int radius = 0;
            if (time < k) { // k 时刻变成 3*3
                radius = 2;
            } else if (time < 2 * k) {  // 2k 时刻变成 1*1
                radius = 1;
            } else {
                radius = 0;
            }
            time++;
            // 原地等待
            if (radius > 0) {
                q.offer(new Node(node.x, node.y, time));
            }
            for (int i = 0; i < 4; i++) {
                int nx = node.x + dirs[i][0];
                int ny = node.y + dirs[i][1];
                if (nx + radius < n && nx - radius >= 0 && ny + radius < n && ny - radius >= 0) {
                    if (!vis[nx][ny] && check(nx, ny, radius)) {
                        q.offer(new Node(nx, ny, time));
                        if (nx == n - 3 && ny == n - 3) {   // 终点
                            return time;
                        }
                        vis[nx][ny] = true;
                    }
                }
            }
        }
        return - 1;
    }

    /**
     * 	检查周围是否有障碍物
     * @param x
     * @param y
     * @param radius
     * @return
     */
    private static boolean check(int x, int y, int radius) {
        for (int i = x - radius; i <= x + radius; i++) {
            for (int j = y - radius; j <= y + radius; j++) {
                if (map[i][j] == '*') { // 障碍物
                    return false;
                }
            }
        }
        return true;
    }


    static class Node {
        int x, y, time;

        public Node(int x, int y, int time) {
            this.x = x;
            this.y = y;
            this.time = time;
        }
    }
}
